package com.springcloud.product.controller;

import com.springcloud.model.base.Res.BaseRes;
import com.springcloud.model.entity.Product;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shengtao
 * @Description:
 * @date 2019/06/04 20:59
 */
@RestController
@Slf4j
public class ProductController {

    @Autowired
    private Environment environment;

    @RequestMapping(method = RequestMethod.POST)
    public BaseRes save(Product product){
        product.setId("11014");
        return BaseRes.success(product);
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public BaseRes list(){
        System.out.println("=============================："+environment.getProperty("product.info.name"));
        List<Product> products = new ArrayList<>();
        for (int i = 0 ; i< 10;i++){
            val product = Product.builder()
                    .id("10"+i)
                    .price(2.3F)
                    .stock(10+i)
                    .name("8008")
                    .build();
            products.add(product);
        }

//        if(products.size() >= 10){
//            throw new RuntimeException("product is too large");
//        }

        return BaseRes.success(products);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public BaseRes delete(@RequestParam String productId){
        return BaseRes.success(null);
    }

    @RequestMapping(method = RequestMethod.GET)
    public BaseRes getName(){
        log.info("<< trace:2 >>");
        return BaseRes.success(environment.getProperty("product.info.name"));
    }

}
